﻿namespace SimpleWebServices.IO
{
    using System;
    using System.IO;

    public static class StreamExtensionMethods
    {
        public static byte[] ToBytes(this Stream stream)
        {
            if (null == stream)
            {
                throw new ArgumentNullException("stream");
            }

            byte[] buffer = new byte[32768];
            using (MemoryStream memory = new MemoryStream())
            {
                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                    {
                        return memory.ToArray();
                    }

                    memory.Write(buffer, 0, read);
                }
            }
        }
    }
}